package com.xianyun.site.iblog.user.executor.query;

import com.alibaba.cola.dto.SingleResponse;
import com.xianyun.common.core.utils.FormatUtils;
import com.xianyun.common.core.utils.StringUtils;
import com.xianyun.common.core.utils.bean.BeanUtils;
import com.xianyun.site.iblog.co.user.SystemUserCO;
import com.xianyun.site.iblog.common.core.exception.UserInfoNotFoundException;
import com.xianyun.site.iblog.convertor.SystemUserConvertor;
import com.xianyun.site.iblog.domain.user.model.SystemUser;
import com.xianyun.site.iblog.dto.user.SystemUserInfoQry;
import com.xianyun.site.iblog.gatewayimpl.database.SystemUserMapper;
import com.xianyun.site.iblog.gatewayimpl.database.domainobject.SystemUserDO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import javax.validation.Valid;

/**
 * @author mac_zyj
 */
@Component
public class SystemUserInfoQryExo {

    @Autowired
    private SystemUserMapper systemUserMapper;

    public SingleResponse<SystemUserCO> execute(@Valid SystemUserInfoQry qry){
        SystemUserDO result = systemUserMapper.getSystemUserVoById(qry.getUid());
        if (null != result){
            SystemUser userInfo = SystemUserConvertor.toEntity(result);
            SystemUserCO viewInfo = new SystemUserCO();
            BeanUtils.copyProperties(userInfo,viewInfo);
            if (StringUtils.isNotEmpty(viewInfo.getMobile())){
                viewInfo.setMobile(FormatUtils.getSecurityPhoneNumber(viewInfo.getMobile()));
            }
            if (null != viewInfo.getStartWorkTime()){
                viewInfo.setWorkingYears(userInfo.getWorkingYears().intValue());
            }
            //TODO 根据业务需求对用户展示信息做获取 比如常用登陆地 .....
            return SingleResponse.of(viewInfo);
        }
        //TODO 定义成系统故障 或者 检查用户状态 查看其是否被封禁或删除
        throw new UserInfoNotFoundException("BIZ_CLOUD_BLOG_USER_INFO");
    }

}
